package sk.atletis.atletika;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;

public class SplashScreen extends Activity 
{
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash_screen);
		
		new GetFirstData().execute();
	}
	
	// Gets data if database is empty
	private class GetFirstData extends AsyncTask<Void, Void, Void> 
	{
		@Override
        protected void onPreExecute() 
        {
            super.onPreExecute();
        }
 
        @Override
        protected Void doInBackground(Void... arg0) 
        {
        	try 
			{
        		// --- Articles ---
        		Cursor curArticles = getContentResolver().query(
        				ArticlesContract.CONTENT_URI_ARTICLES, 
        				ArticlesContract.PROJECTION, 
        				null, 
        				null, 
        				ArticlesContract.SORT_DEFAULT);

        		if (curArticles.getCount() == 0) 
        		{
        			JsonHandler hArticles = new JsonHandler(SplashScreen.this , "http://atletikasvk.sk/DataOutput/Articles");
    				hArticles.updateArticles();
        		}
        		
        		// --- Competitions ---
        		Cursor curCompetitions = getContentResolver().query(
        				CompetitionsContract.CONTENT_URI_COMPETITIONS, 
        				CompetitionsContract.PROJECTION, 
        				null, 
        				null, 
        				CompetitionsContract.SORT_DEFAULT);
        		
        		if (curCompetitions.getCount() == 0)
        		{
        			JsonHandler hCompetitions = new JsonHandler(SplashScreen.this , "http://atletikasvk.sk/DataOutput/Competitions");
        			hCompetitions.updateCompetitions();
        		}
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
    		
    		return null;
        }
 
        @Override
        protected void onPostExecute(Void result) 
        {
            super.onPostExecute(result);
            //Intent i = new Intent(SplashScreen.this, HomeActivity.class);
            Intent i = new Intent(SplashScreen.this, MainActivity.class);
            startActivity(i);
            finish();
        }
    }

	

}
